package se_adxslot

import (
	"fmt"
	"strings"
	"wx-api/adx-api/cache"
	"wx-api/adx-api/errmsg"
	"wx-api/adx-api/handlers"
	"wx-api/adx-api/model/m_adxslot"
	"wx-api/adx-api/services/comm"
	"wx-api/adx-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxslot.List(id, name, "", flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	tmplist := []m_adxslot.AdxSlot{}
	ttt := data.Data.(*[]m_adxslot.AdxSlot)
	for _, v := range *ttt {
		tmp := v
		ainfo, ok := cache.CacheAdxListMapNGet(v.AdxId)
		if ok {
			tmp.AdxName = ainfo.Name
		} else {
			tmp.AdxName = "-"
		}
		sinfo, ok := cache.CacheAdxAppMapNGet(v.Mid)
		if ok {
			tmp.MidName = sinfo.Name
		} else {
			tmp.MidName = "-"
		}
		tinfo, ok := cache.CacheAdxTemplateMapNGet(v.TemplateId)
		if ok {
			tmp.TemplateName = tinfo.CreateTemplateId
		} else {
			tmp.TemplateName = "-"
		}
		tmplist = append(tmplist, tmp)
	}
	data.Data = tmplist
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_adxslot.AdxSlot
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	if form.AdxId == "" {
		c.SetErrorMsg(nil, errmsg.GetError("所属adx is error"))
		return
	}
	adxinfo, ok := cache.CacheAdxListMapGet(form.AdxId)
	if !ok {
		c.SetErrorMsg(nil, errmsg.GetError("所属adx is error"))
		return
	}
	form.AdxId = adxinfo.Id.Hex()
	_, ok = cache.CacheAdxAppMapNGet(form.Mid)
	if form.Mid == "" || !ok {
		c.SetErrorMsg(nil, errmsg.GetError("所属应用不存在"))
		return
	}
	slot_type_ck := utils.CkslotType(form.SlotType)
	if form.SlotType == "" || !slot_type_ck {
		c.SetErrorMsg(nil, errmsg.GetError("广告位类型选择错误"))
		return
	}
	form.Code = utils.RandomString(6)
	if form.SlotId == "" {
		form.SlotId = utils.RandomString(6)
	}
	if form.SlotName == "" {
		form.SlotName = form.Name
	}
	id, err := m_adxslot.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheAdxSlotMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_adxslot.AdxSlot
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	sspinfo, err := m_adxslot.GetByCk(form.Id.Hex(), adx_uid)
	if err != nil || sspinfo == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	err = m_adxslot.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheAdxSlotMapUpdate(sspinfo.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxslot.GetByCk(id, adx_uid)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxslot.GetByCk(id, adx_uid)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_adxslot.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheAdxSlotMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxslot.GetByCk(id, adx_uid)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_adxslot.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheAdxSlotMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

// 获取所有所属应用
func All(c *handlers.Controller) {
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无缓存，请联系管理员"))
		return
	}
	adx_id := c.GetString("adx_id")
	if adx_id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_ids := strings.Split(adx_id, ",")
	appids := []string{}
	for _, id := range adx_ids {
		tmp_ids := cache.CacheAdxSlotResAdxUidGet(id)
		appids = append(appids, tmp_ids...)
	}
	app_arr := []m_adxslot.AdxSlot{}
	for _, id := range appids {
		info, ok := cache.CacheAdxSlotMapGet(id)
		if !ok {
			continue
		}
		ainfo, ok := cache.CacheAdxListMapGet(info.AdxId)
		if !ok {
			continue
		}
		minfo, ok := cache.CacheAdxAppMapGet(info.Mid)
		if !ok {
			continue
		}
		app_arr = append(app_arr, m_adxslot.AdxSlot{
			Id:         info.Id,
			Code:       info.Code,
			Name:       info.Name,
			AdxId:      info.AdxId,
			AdxName:    ainfo.Name,
			Mid:        info.Mid,
			MidName:    minfo.Name,
			SlotId:     info.SlotId,
			SlotType:   info.SlotType,
			TemplateId: info.TemplateId,
		})
	}
	c.SetErrorMsg(app_arr, errmsg.SuccessMsg())
	return
}
